package handlers

import (
	"encoding/json"
	"fav-backend/cache"
	"fav-backend/models"
	"fav-backend/pkg"
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
)

func Login(c *gin.Context) {
	username := c.PostForm("username")
	password := c.PostForm("password")
	if username == "" || password == "" {
		c.JSON(200, gin.H{

			"code": 400,
			"msg":  "账号或密码不能为空",
		})
		return
	}

	var user models.User
	redis_key := "redis_login_" + username
	redisContent, _ := cache.Rdb.Get(cache.Ctx, redis_key).Int()
	if redisContent >= 3 {
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "登录失败三次,禁止登录",
		})
		return
	}
	err := models.DB.Where("username=?", username).First(&user).Error
	if err != nil {
		c.JSON(200, gin.H{

			"code": 400,
			"msg":  "用户不存在",
		})
		return
	}
	if user.ID == 0 {
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "用户不存在",
		})
		return
	}
	if pkg.Md5(password) != user.Password {
		cache.Rdb.Incr(cache.Ctx, redis_key)
		if redisContent == 2 {
			cache.Rdb.Set(cache.Ctx, redis_key, 3, time.Hour*2)
			c.JSON(200, gin.H{
				"code": 400,
				"msg":  "登录失败已锁定,两小时后解锁",
			})
			return
		}
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "密码错误",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "登录成功",
		"user": user,
	})
}
func List(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	pageSize, _ := strconv.Atoi(c.DefaultQuery("pageSize", "3"))
	offset := (page - 1) * pageSize
	/*
	   SELECT
	   text_colls.id as id,
	   colls.title as Title,
	   colls.content as Content,
	   text_colls.username as Username,
	   text_colls.sex as Sex
	   FROM text_colls

	   LEFT JOIN colls ON colls.id=text_colls.id
	*/
	var arr []models.CollList
	redis_key := "redis_list_" + strconv.Itoa(page)
	redisData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if redisData == "" {
		models.DB.Table("text_colls").Select(
			"text_colls.id as id",
			"colls.title as Title",
			"colls.content as Content",
			"text_colls.username as Username",
			"text_colls.sex as Sex",
		).
			Joins("LEFT JOIN colls ON colls.id=text_colls.id").Offset(offset).
			Limit(pageSize).Find(&arr)
		jsonData, _ := json.Marshal(arr)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(redisData), &arr)
	}
	var total int64
	models.DB.Model(&models.TextColl{}).Count(&total)
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "查询成功",
		"list":  arr,
		"total": total,
	})
}
func Detail(c *gin.Context) {
	id := c.Query("id")
	var arr models.CollList
	redis_key := "redis_detail_" + id
	redisData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if redisData == "" {
		models.DB.Table("text_colls").Select(
			"text_colls.id as id",
			"colls.title as Title",
			"colls.content as Content",
			"text_colls.username as Username",
			"text_colls.sex as Sex",
		).
			Joins("LEFT JOIN colls ON colls.id=text_colls.id").Where("text_colls.id=?", id).Debug().Find(&arr)
		jsonData, _ := json.Marshal(arr)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(redisData), &arr)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "详情成功",
		"list": arr,
	})
}

func UserRoleList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	pageSize, _ := strconv.Atoi(c.DefaultQuery("pageSize", "2"))
	offset := (page - 1) * pageSize
	/*
		SELECT
		roles.uid as id,
		users.username as Username,
		users.password as Password,
		roles.role_name  as RoleName,
		roles.role_sex as RoleSex
		FROM roles
		LEFT JOIN users ON users.id=roles.uid

	*/
	var role []models.UserRole
	rid_key := "redis_list_" + strconv.Itoa(page)
	RedisData, _ := cache.Rdb.Get(cache.Ctx, rid_key).Result()
	if RedisData == "" {
		models.DB.Table("roles").
			Select(
				"roles.uid as Rid",
				"users.username as Username",
				"users.password as Password",
				"roles.role_name  as RoleName",
				"roles.role_sex as RoleSex",
			).
			Joins("LEFT JOIN users ON users.id=roles.uid").
			Offset(offset).Limit(pageSize).Find(&role)

		jsonData, _ := json.Marshal(role)
		cache.Rdb.Set(cache.Ctx, rid_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(RedisData), &role)
	}
	var total int64
	models.DB.Model(&models.Role{}).Count(&total)
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "查询成功",
		"list":  role,
		"total": total,
	})
}
func UserRoleListDetail(c *gin.Context) {
	id := c.Query("id")
	var role models.UserRole
	rid_key := "redis_list_" + id
	RedisData, _ := cache.Rdb.Get(cache.Ctx, rid_key).Result()
	if RedisData == "" {
		models.DB.Table("roles").
			Select(
				"roles.uid as Rid",
				"users.username as Username",
				"users.password as Password",
				"roles.role_name  as RoleName",
				"roles.role_sex as RoleSex",
			).
			Joins("LEFT JOIN users ON users.id=roles.uid").
			Where("roles.id=?", id).Find(&role)

		jsonData, _ := json.Marshal(role)
		cache.Rdb.Set(cache.Ctx, rid_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(RedisData), &role)
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"list": role,
	})
}
